/**
* @Description: http response
* @Author: jinyidong
* @Date: 2021/5/14
* @Version V1.0
 */
package util

var (
	OK             = response(0, "ok")
	UnKnownError   = response(-1, "unknown error")
	ParseError     = response(-32700, "parse error")
	InvalidRequest = response(-32600, "invalid request")
	MethodNotFound = response(-32601, "method not found")
	InvalidParams  = response(-32602, "invalid params")
	InternalError  = response(-32603, "internal error")
	ServerError    = response(-32000, "server error")
)

// 构造函数
func response(code int, msg string) Response {
	return Response{
		Code:   code,
		Msg:    msg,
		Result: nil,
	}
}

type Response struct {
	Code   int         `json:"error_code"`
	Msg    string      `json:"error_message"`
	Result interface{} `json:"result"` // 返回数据
}

// 追加响应数据
func (res *Response) WithResult(result interface{}) Response {
	return Response{
		Code:   res.Code,
		Msg:    res.Msg,
		Result: result,
	}
}

// 自定义响应信息
func (res *Response) WithMsg(message string) Response {
	return Response{
		Code:   res.Code,
		Msg:    message,
		Result: res.Result,
	}
}
